package org.dromara.job.processors;

import lombok.extern.slf4j.Slf4j;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.common.websocket.constant.WebSocketConstants;
import org.dromara.common.websocket.dto.WebSocketMessageDto;
import org.springframework.stereotype.Component;
import tech.powerjob.worker.core.processor.ProcessResult;
import tech.powerjob.worker.core.processor.TaskContext;
import tech.powerjob.worker.core.processor.sdk.BasicProcessor;

import java.util.Random;
import java.util.stream.IntStream;

@Slf4j
@Component
public class DmsAlarmMessage implements BasicProcessor {


    @Override
    public ProcessResult process(TaskContext context) throws Exception {

        Random random = new Random();
        int int1 = random.nextInt(2);
        int status = int1 == 1 ? 0 : 99;
        // TODO: 模拟数据，正式开始开发改为接口
        String str = "{\"ID\":168489,\"EquipCode\":\"DXCA0106\",\"AlarmCode\":\"10000670\", \"AlarmText\":\"故障！加热单元报警故障，请到温度画面查看具体故障！\", " +
            "\"AlarmTime\":\"2023-11-11 19:15:18\", \"ClearTime\":\"2023-11-12 03:07:58\", \"Active\":"+int1+", \"UpdateTime\":\"2023-11-11 19:15:18\",\"DataStatus\":"+status+",\"AlarmLevel\":1,\"LastArchiveTime\":\"2023-11-11 19:15:18\", \"PlantNO\":\"\", \"DataFlag\":0 }";

        int int2 = random.nextInt(2);
        int status1 = int1 == 1 ? 0 : 99;

        String str1 = "{\"ID\":36737,\"EquipCode\":\"DXCA0106\",\"AlarmCode\":\"10000671\", \"AlarmText\":\"故障！加热接触器没有吸合，加热控制被停止！\", " +
            "\"AlarmTime\":\"2023-11-11 19:15:18\", \"ClearTime\":\"2023-11-12 03:07:58\", \"Active\":"+int2+", \"UpdateTime\":\"2023-11-11 19:15:18\",\"DataStatus\":"+status1+",\"AlarmLevel\":1,\"LastArchiveTime\":\"2023-11-11 19:15:18\", \"PlantNO\":\"\", \"DataFlag\":0 }";

        int int3 = random.nextInt(2);
        int status2 = int1 == 1 ? 0 : 99;

        String str2 = "{\"ID\":36737,\"EquipCode\":\"DXCA0108\",\"AlarmCode\":\"10000970\", \"AlarmText\":\"故障！台板降动作速度过快(＞300mm/S)！\", " +
            "\"AlarmTime\":\"2023-11-11 20:27:28\", \"ClearTime\":\"2023-11-12 01:19:34\", \"Active\":"+int3+", \"UpdateTime\":\"2023-11-11 20:27:28\",\"DataStatus\":"+status2+",\"AlarmLevel\":1,\"LastArchiveTime\":\"2023-11-11 20:27:28\", \"PlantNO\":\"\", \"DataFlag\":0 }";

        int int4 = random.nextInt(2);
        int status3 = int1 == 1 ? 0 : 99;

        String str3 = "{\"ID\":36737,\"EquipCode\":\"DXCA0111\",\"AlarmCode\":\"10001305\", \"AlarmText\":\"故障！加热单元报警故障，请到温度画面查看具体故障！\", " +
            "\"AlarmTime\":\"2023-11-11 20:44:15\", \"ClearTime\":\"2023-11-12 00:50:31\", \"Active\":"+int4+", \"UpdateTime\":\"2023-11-11 20:44:15\",\"DataStatus\":"+status3+",\"AlarmLevel\":1,\"LastArchiveTime\":\"2023-11-11 20:44:15\", \"PlantNO\":\"\", \"DataFlag\":0 }";
        // 获取报警信息
        WebSocketMessageDto broadcastMessage = new WebSocketMessageDto();
        broadcastMessage.setMessage("["+str+","+str1+","+str2+","+str3+"]");
        RedisUtils.publish(WebSocketConstants.WEB_SOCKET_TOPIC,broadcastMessage);

        return new ProcessResult(true);
    }

}


